<?php

namespace App\Admin\Actions\Waybill;

use Encore\Admin\Actions\BatchAction;
use Illuminate\Database\Eloquent\Collection;
use App\Models\Waybill;
use App\Models\Goods;

class BatchRecost extends BatchAction
{
    public $name = '批量重算';

    public function handle(Collection $collection)
    {
        foreach ($collection as $model)
        {
            $waybill = Waybill::where('id', $model->id)->with('goods')->first();

            $params = [
                'goods_price' => sprintf('%.2f', $waybill->goods['sale_price'] * $waybill->goods_num),
                'cost_amount' => sprintf('%.2f', $waybill->goods['cost_price'] * $waybill->goods_num),
                'profit_amount' => sprintf('%.2f', ($waybill->goods['sale_price'] * $waybill->goods_num) - ($waybill->goods['cost_price'] * $waybill->goods_num)),
                'status' => 1,
                'updated_at' => date('Y-m-d H:i:s'),
            ];

            Waybill::where('id', $model->id)->update($params);
        }

        return $this->response()->success('重算完成')->refresh();
    }

}